﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EBEInventory_DAL;

using System.Data.Objects;

public partial class Inventory_Items_Management_Default : System.Web.UI.Page
{
    InventoryEntities ctx = null;
    string parCatID;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string opType = Server.UrlDecode(Request.QueryString["Operation"]);
            parCatID  = Server.UrlDecode(Request.QueryString["ParentID"]);
            ViewState["ParentCatID"] = parCatID;

            ctx = InitConnection.Initialize();

            string CategoryID = ctx.CreateQuery<string>("SELECT VALUE InventoryEDM.Store.GetNewCategoryID(@CategoryID) FROM {0}",
            new ObjectParameter("CategoryID", parCatID)).First();

            if (string.Compare(opType, "Register") == 0)
            {
                ViewState["Operation"] = btn_RegCategory.Text = opType;
                txt_NewCatID.Enabled = false;

                txt_NewCatID.Text = CategoryID;
            }

            else if (string.Compare(opType, "Edit") == 0)
            {
                ViewState["Operation"] = btn_RegCategory.Text = opType;
                txt_NewCatID.Enabled = false;
            }

            else if (string.Compare(opType, "Delete") == 0)
            {
                ViewState["Operation"] = btn_RegCategory.Text = opType;
                txt_NewCatID.Enabled = false;
            }

            //category cat = (category)Session["SelectedCategory"];

        }

        else
        {
            ctx = InitConnection.Initialize();
        }
    }
    protected void btn_RegCategory_Click(object sender, EventArgs e)
    {
        parCatID = ViewState["ParentCatID"].ToString();
        Dictionary<string, string> cat = new Dictionary<string, string>();

        //string CategoryID = ctx.CreateQuery<string>("SELECT VALUE InventoryEDM.Store.GetNewCategoryID(@CategoryID) FROM {0}",
        //    new ObjectParameter("CategoryID", parCatID)).First();

        cat["catID"] = txt_NewCatID.Text;
        cat["catName"] = txt_NewCatName.Text;
        cat["catParentID"] =parCatID ;

        if (string.Compare(ViewState["Operation"].ToString(), "Register") == 0)
        {
            CatOps.AddCategory(cat , ctx);
        }

        else if (string.Compare(ViewState["Operation"].ToString(), "Edit") == 0)
        {
            //InvOps.EditInventory(cat, ctx);
        }

        else if (string.Compare(ViewState["Operation"].ToString(), "Delete") == 0)
        {
           // InvOps.DeleteInventory(cat["ID"], ctx);
        }

        Response.Redirect("~/Inventory/Items Management//ListItems.aspx");
        
    }
}